package thesis;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import de.bwaldvogel.liblinear.InvalidInputDataException;
import de.bwaldvogel.liblinear.Predict;

public class ThesisHackCode {

	public static void main(String[] args) throws IOException,
			InvalidInputDataException {
		// label of classes
		ArrayList<String> label = new ArrayList<String>();
		label.add("1");
		label.add("2");
		label.add("3");
		label.add("4");
		label.add("5");

		// number of models
		ArrayList<String> model = new ArrayList<String>();
		for (int i = 0; i < label.size() - 1; i++) {
			for (int j = i + 1; j < label.size(); j++) {
				model.add((i + 1) + "_" + (j + 1));
			}
		}

		// array of original file to array to check accuracy
		ArrayList<String> accuracyTrain = new ArrayList<String>();
		// array of classified file to array to check accuracy
		ArrayList<Integer> accuracyTest = new ArrayList<Integer>();

		String fileTestName = "1_2.test"; // test file which contains 1 or a lot
											// of test file in LibSVM format
		String modelFile = null;
		String outputFile = null;

		// read file contains 1 or a lot of test file in LibSVM format
		ArrayList<String> fileNames = new ArrayList<String>();
//		System.out.println(readArrayFile(fileTestName));
		fileNames.addAll(readArrayFile(fileTestName));

		ArrayList<String> realFilenames = new ArrayList<String>(); // array
																	// contain
																	// only 1
																	// test in
																	// each file
		
		for (int i = 0; i < fileNames.size(); i++) {
			// add label of original file to array to check accuracy
			if (fileNames.get(i).contains(" ")) {
				accuracyTrain.add(fileNames.get(i).substring(0,
						fileNames.get(i).indexOf(" ")));
			} else {
				accuracyTrain.add(fileNames.get(i));
			}
		}
		
		String[] predictArg = new String[4]; 
		predictArg[0] = fileTestName;
		predictArg[1] = "1_2.train.model";
		predictArg[2] = "1_2.out";
		predictArg[3] = "1";
		
		Predict mainPredict = new Predict();
		mainPredict.main(predictArg);
		System.out.println("mainPredict.predictLabelArray.size(): " + mainPredict.mainPredictLabelArray.size());
		//predict
		for(int i = 0; i < fileNames.size(); i++){ //fileNames.size()
//			System.out.println("predict " + (i+1) + " : " + mainPredict.predictLabelArray.get(i));
			int beforeOutputFile = 0;
			
			for (int j = 2; j < label.size(); j++) {
				if (j == 2) {
					String[] predictArg2 = new String[4];
					Predict predict = new Predict();
					System.out.println("predict " + (i+1) + " ---------------");
					predictArg2[0] = fileTestName; //not necessary
					predictArg2[1] = mainPredict.mainPredictLabelArray.get(i) + "_" + (j+1) + ".train.model";
					predictArg2[2] = "out"; //not necessary
					predictArg2[3] = "2"; 
					
//					System.out.println("singleFile : " + fileNames.get(i));
					System.out.println("beforeOutputFile : " + beforeOutputFile);
					System.out.println("model : " + mainPredict.mainPredictLabelArray.get(i) + "_" + (j+1) + ".train.model");
					
					predict.singleFile = fileNames.get(i);
					predict.predictLabelArray.clear();
					predict.main(predictArg2);					
					beforeOutputFile = predict.predictLabelArray.get(0);
					System.out.println("predict.predictLabelArray : " + predict.predictLabelArray);
				} else {
					String[] predictArg2 = new String[4];
					Predict predict = new Predict();
					
					predictArg2[0] = fileTestName; //not necessary
					predictArg2[1] = beforeOutputFile + "_" + (j+1) + ".train.model";
					predictArg2[2] = "out"; //not necessary
					predictArg2[3] = "2"; 
					
//					System.out.println("singleFile : " + fileNames.get(i));
					System.out.println("beforeOutputFile : " + beforeOutputFile);
					System.out.println("model : " + mainPredict.mainPredictLabelArray.get(i) + "_" + (j+1) + ".train.model");
					
					predict.singleFile = fileNames.get(i);
					predict.predictLabelArray.clear();
					predict.main(predictArg2);
					beforeOutputFile = predict.predictLabelArray.get(0);
					System.out.println("predict.predictLabelArray : " + predict.predictLabelArray);
					if (j == label.size() - 1) {
						accuracyTest.add(predict.predictLabelArray.get(0));
					}
					
				}
				
			}
		}

		// predict
//		for (int i = 0; i < 10; i++) { // i < fileNames.size()
//			String[] predictArg2 = new String[3];
//			String testFileArg = "";
//			String modelFileArg = "";
//			String outFileArg = "";
//			String beforeOutputFile = "";
//
//			// default 1st & 2nd labels, start from 3rd label
//			for (int j = 1; j < label.size(); j++) {
//				if (j == 1) {
//					testFileArg = realFilenames.get(i);
//					modelFileArg = "1_2.train.model";
//					outFileArg = realFilenames.get(i).split("\\.")[0]
//							+ "_1_2.out";
//					beforeOutputFile = outFileArg;
//
//					predictArg2[0] = testFileArg;
//					predictArg2[1] = modelFileArg;
//					predictArg2[2] = outFileArg;
//
//					// System.out.println("predictArg: " + predictArg);
//
//					Predict predict = new Predict();
//					predict.main(predictArg2);
//				} else {
//					// add label of output file to array to check accuracy
//					if (j == label.size() - 1) {
//						accuracyTest.add(getModel(beforeOutputFile));
//					}
//
//					testFileArg = realFilenames.get(i);
//					modelFileArg = getModel(beforeOutputFile) + "_" + (j + 1)
//							+ ".train.model";
//					System.out.println("modelFileArg: " + modelFileArg);
//					outFileArg = realFilenames.get(i).split("\\.")[0] + "_"
//							+ getModel(beforeOutputFile) + "_" + (j + 1)
//							+ ".out";
//					System.out.println("beforeOutputFile: " + beforeOutputFile);
//					System.out.println("outFileArg: " + outFileArg);
//					beforeOutputFile = outFileArg;
//
//					predictArg[0] = testFileArg;
//					predictArg[1] = modelFileArg;
//					predictArg[2] = outFileArg;
//
//					System.out.println("predictArg: " + predictArg);
//
//					Predict predict = new Predict();
//					predict.main(predictArg);
//				}
//			}
//		}
		System.out.println("accuracyTrain.size(): " + accuracyTrain.size());
		System.out.println("accuracyTest.size(): " + accuracyTest.size());
		double accuracy = 0;
		int countAccuracy = 0;
		// if(accuracyTest.size() == accuracyTrain.size()){
		for (int i = 0; i < accuracyTest.size(); i++) {
			System.out.println("accuracyTrain +  accuracyTest: "
					+ accuracyTrain.get(i) + "+" + accuracyTest.get(i));
			if (accuracyTrain.get(i).equals(accuracyTest.get(i))) {
				countAccuracy++;
			}
		}
		accuracy = countAccuracy / accuracyTest.size();
		System.out.println("countAccuracy: " + countAccuracy);
		System.out.println("Do chinh xac: " + accuracy);
		// }

		// System.out.println("Nhap thong so: ");
		//
		// BufferedReader buf = new BufferedReader (new InputStreamReader
		// (System.in));
		// String line = buf.readLine ();
		//
		// String input[] = line.split(" ");
		// System.out.println(line);
		//
		// Predict predict = new Predict();
		// predict.main(input);

	}

	// read before oue file to get current label, then add next label to get
	// exactly 2 label which is need to be classify
	static String getModel(String testFileName) {
		String temp = "";
		try (BufferedReader br = new BufferedReader(
				new FileReader(testFileName))) {
			String sCurrentLine;
			while ((sCurrentLine = br.readLine()) != null) {
				temp += sCurrentLine;
				temp = temp.substring(0, 1);
				break;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("Label: " + temp);

		return temp;
	}

	static String readFile(String fileName) {
		String temp = "";
		try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
			String sCurrentLine;
			while ((sCurrentLine = br.readLine()) != null) {
				temp += sCurrentLine + "\n";
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return temp;
	}

	static void writeFile(String fileName, String writeString) {
		try {
			File fileDir = new File(fileName);
			BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(fileDir), "UTF8"));

			out.append(writeString);

			out.flush();
			out.close();
		} catch (UnsupportedEncodingException e) {
			System.out.println(e.getMessage());
		} catch (IOException e) {
			System.out.println(e.getMessage());
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	static ArrayList<String> readArrayFile(String fileName) {
		ArrayList<String> temp = new ArrayList<String>();
		try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
			String sCurrentLine;
			while ((sCurrentLine = br.readLine()) != null) {
				temp.add(sCurrentLine);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return temp;
	}
}
